package com.bootdo.api;

import com.bootdo.backlist.dto.AppuserDTO;
import com.bootdo.backlist.dto.InfoDTO;
import com.bootdo.backlist.service.AppuserService;
import com.bootdo.backlist.service.InfoService;
import com.bootdo.common.enums.ResultEnum;
import com.bootdo.common.utils.R;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * @author Zengcx
 * @date 2019-04-15 15:01
 */
@RestController
@RequestMapping("/api/info")
public class ApiInfoController {

    @Autowired
    private InfoService infoService;

    @Autowired
    private AppuserService appuserService;

    /**
     * 基础信息接口
     * @return
     */
    @GetMapping("/list")
    public R list(){
        List<InfoDTO> list = infoService.list(new HashMap<>());
        if (!CollectionUtils.isEmpty(list)){
            return R.ok().put("data",list);
        }
        return R.error(ResultEnum.DATA_NOT_EXIST.getMessage());
    }

    /**
     * 修改用户积分
     * @param userId
     * @return
     */
    @PostMapping("/updateIntegral")
    public R updateIntegral(@RequestParam("userId") Long userId){
        if (userId == null){
            return R.error(ResultEnum.PARAM_ERROR.getMessage());
        }
        List<InfoDTO> list = infoService.list(new HashMap<>());
        AppuserDTO appuserDTO = appuserService.get(userId);
        appuserDTO.setIntegral(appuserDTO.getIntegral().add(new BigDecimal(list.get(0).getGiveNum())));
        return appuserService.update(appuserDTO);
    }
}
